using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Comteck.Winforms.Print {
  public class UnknownHelper {
    private string m_PrintName;

    private BasePrintHelper m_Helper;


    public UnknownHelper(string printname) {
      m_PrintName = printname;

      m_Helper = new BasePrintHelper();

      m_Helper.Open(m_PrintName);

      m_Helper.SetLeft(0);
      m_Helper.SetAlignMode(eTextAlignMode.Middle);
    }

    #region IPrintHelper 成员




    private object lockobj = new object();

    /// <summary>
    /// 设置
    /// </summary>
    /// <param name="content"></param>
    /// <param name="fontsize"></param>
    /// <param name="paperwidth"></param>
    /// <returns></returns>
    public EnumResult PrintTextByPaperWidth(string content, int fontsize, int paperwidth) {

      lock (lockobj) {
        bool r = m_Helper.Close();

        if (!r) {
          return EnumResult.Error;
        }

        r = m_Helper.Open(m_PrintName);

        if (!r) {
          return EnumResult.Error;
        }


        r = m_Helper.StartPrint();

        if (!r) {
          return EnumResult.Error;
        }


        r = m_Helper.SetFontSize(fontsize);

        if (!r) {
          return EnumResult.Error;
        }


        r = m_Helper.PrintText(content);

        if (!r) {
          return EnumResult.Error;
        }

        r = m_Helper.EndPrint();

        if (!r) {
          return EnumResult.Error;
        }


        return EnumResult.Success;
      }

    }

    /// <summary>
    /// 打印位图
    /// </summary>
    /// <param name="path"></param>
    /// <returns></returns>
    public EnumResult PrintBitMap(string path) {
      lock (lockobj) {

        bool r = m_Helper.StartPrint();

        if (!r) {
          return EnumResult.Error;
        }

        r = m_Helper.PrintImageFile(path);

        if (!r) {
          return EnumResult.Error;
        }

        r = m_Helper.EndPrint();

        if (!r) {
          return EnumResult.Error;
        }

        return EnumResult.Success;
      }

    }


    public EnumResult PrintBarCode(string code) {
      lock (lockobj) {

        bool r = m_Helper.StartPrint();

        if (!r) {
          return EnumResult.Error;
        }

        r = m_Helper.PrintBarcode(code);

        if (!r) {
          return EnumResult.Error;
        }

        r = m_Helper.EndPrint();

        if (!r) {
          return EnumResult.Error;
        }

        return EnumResult.Success;
      }
    }

    public EnumResult BeginPrint(EnumPrintType type, EnumPrintMode mode = EnumPrintMode.Receipt, EnumPrintInterfaceType itype = EnumPrintInterfaceType.USB, bool isimage = false) {
      //if (type == EnumPrintType.PagerSkip)
      //{
      //    BlackFeed();
      //}
      return EnumResult.Success;
    }


    /// <summary>
    /// 切纸
    /// </summary>
    /// <returns></returns>
    public EnumResult CutPager() {
      lock (lockobj) {

        bool r = m_Helper.StartPrint();

        if (!r) {
          return EnumResult.Error;
        }

        r = m_Helper.Cut();

        r = m_Helper.EndPrint();

        return EnumResult.Success;
      }
    }

    public EnumResult Feed(int l) {
      lock (lockobj) {
        bool r = m_Helper.StartPrint();

        if (!r) {
          return EnumResult.Error;
        }

        r = m_Helper.Feed(l);

        r = m_Helper.EndPrint();

        return EnumResult.Success;

      }
    }

    /// <summary>
    /// 黑标走纸
    /// </summary>
    /// <returns></returns>
    public EnumResult BlackFeed() {
      lock (lockobj) {
        bool r = m_Helper.StartPrint();

        if (!r) {
          return EnumResult.Error;
        }

        r = m_Helper.BlackFeed();

        r = m_Helper.EndPrint();

        return EnumResult.Success;

      }
    }

    #endregion

    #region IDisposable 成员

    public void Dispose() {
      m_Helper.Dispose();
    }

    #endregion

    #region ICashDrawerHelper 成员

    /// <summary>
    /// 开钱箱
    /// </summary>
    /// <returns></returns>
    public EnumResult OpenCashDrawer() {
      lock (lockobj) {


        bool r = m_Helper.StartPrint();

        if (!r) {
          return EnumResult.Error;
        }

        string command = "1B 70 00 64 64";
        //"1B 70 6D 31 32";

        r = m_Helper.SendCommand(command);

        if (!r) {
          return EnumResult.Error;
        }


        r = m_Helper.EndPrint();

        if (!r) {
          return EnumResult.Error;
        }


        return EnumResult.Success;
      }
    }

    /// <summary>
    /// 开钱箱通过命令
    /// </summary>
    /// <param name="cmd"></param>
    /// <returns></returns>
    public EnumResult OpenCashDrawer(string cmd) {
      lock (lockobj) {


        bool r = m_Helper.StartPrint();

        if (!r) {
          return EnumResult.Error;
        }

        string command = cmd;

        r = m_Helper.SendCommand(command);

        if (!r) {
          return EnumResult.Error;
        }

        r = m_Helper.EndPrint();

        if (!r) {
          return EnumResult.Error;
        }

        return EnumResult.Success;
      }
    }

    #endregion
  }
}

